import { defineStore } from "pinia";
import { UserState } from "../interface";
import piniaPersistConfig from "@/stores/helper/persist";

export const useUserStore = defineStore({
  id: "user",
  state: (): UserState => ({
    token: localStorage.getItem("token") || "",
    userInfo: {
      issuperadmin: false,
      permission: {
        call: 1,
        managedevice: 1,
        playmusic: 1,
        setvol: 1,
        stopcall: 1,
        stopplaymusic: 1,
        uploadmusic: 1,
        uploadtimetask: 1,
      },
      secret: "",
    },
  }),
  getters: {},
  actions: {
    /**
     * @use 保存token
     */
    setToken(token: string) {
      this.token = token;
    },

    /**
     * @use 保存用户信息
     * @param userInfo 用户名信息
     */
    setUserInfo(userInfo: UserState["userInfo"]) {
      this.userInfo = userInfo;
      this.userInfo.permission = JSON.parse(
        userInfo.permission as unknown as string
      );
    },
  },
  persist: piniaPersistConfig(`user`),
});
